#!/usr/bin/env python3
from intelhex import hex2bin
import os
import subprocess
import re
import shutil
hex2bin('components/chip_LE5010/ble_sdk/soc/arm_cm/le501x/bin/ll_fw_legacy.hex','boards/LE5010_evb/bootimgs/stack',0x18007000,pad=0xff)
hex2bin('components/chip_LE5010/ble_sdk/soc/arm_cm/le501x/bin/ll_fw.hex','boards/LE5010_evb/bootimgs/stack5',0x18007000,pad=0xff)

def gen_init_bin(path):
    os.chdir('solutions/arm_dummy_boot_demo')
    subprocess.Popen('make').wait()
    os.chdir('../../')
    with open(path,"wb") as out:
        subprocess.Popen(['cat','boards/LE5010_evb/bootimgs/info_sbl.bin','solutions/arm_dummy_boot_demo/boot.bin',],stdout=out).wait()

gen_init_bin('boards/LE5010_evb/bootimgs/init')

shutil.copyfile('components/chip_LE5010/package.yaml','components/chip_LE5010/package.yaml.bak')

with open('components/chip_LE5010/package.yaml','r') as f:
    content = f.read()
    new_content = re.sub('^.+- ll_fw_legacy$','    - ll_fw',content,flags = re.MULTILINE)
with open('components/chip_LE5010/package.yaml','w') as f:
    f.write(new_content)

gen_init_bin('boards/LE5010_evb/bootimgs/init5')

shutil.move('components/chip_LE5010/package.yaml.bak','components/chip_LE5010/package.yaml')


